<?php
/**
 *   駿濤 （xjt@ourstu.com）
 */
namespace Pingxx\Controller;

use Think\Controller;


class CallbackController extends Controller
{
    public function index()
    {

        $link = M('order_link')->where(array('order_id' => $_GET['out_trade_no']))->find();

        switch($link['app']){
            case 'store':
                $this->redirect('store/center/orders');
                break;
            default: $this->redirect('pingxx/index/order', array('id' => $_GET['out_trade_no']));
        }


    }


    public function webhooks()
    {

        $event = json_decode(file_get_contents("php://input"), true);
      //  file_put_contents('aaa.txt',file_get_contents("php://input"));
        // 对异步通知做处理
        if (!isset($event['type'])) {
            header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
            exit("fail");
        }
        switch ($event['type']) {
            case "charge.succeeded":
                // 开发者在此处加入对支付异步通知的处理代码
                $order_id = D('Pingxx/PingxxCharge')->addCharge($event['data']['object']);
                $link = D('order_link')->where(array('order_id' => $order_id))->find();
                $result = R(ucfirst($link['app']) . '/Pingxx/afterPay', array('event' => $event), 'Widget');

                //end

                header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
                break;
            case "refund.succeeded":
                // 开发者在此处加入对退款异步通知的处理代码
                header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
                break;
            default:
                header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
                break;
        }
    }

    public function getChargeStatus(){
        $order_id = I('get.order_id');
        $status = D('Pingxx/PingxxCharge')->getChargeStatus($order_id);
        exit(json_encode(array('status'=>$status?1:0)));
    }


}


